import { defineStore } from "pinia";
import { ref, computed } from "vue";
import type { Menu, CreateMenuRequest, UpdateMenuRequest } from "@/types/menu";
import { menuApi } from "@/apis/menu";

export const useMenuStore = defineStore("menu", () => {
  // State
  const menus = ref<Menu[]>([]);
  const loading = ref(false);
  const error = ref<string | null>(null);
  const currentMenu = ref<Menu | null>(null);
  const menuDetail = ref<Menu | null>(null);

  // Getters
  const menuCount = computed(() => menus.value.length);

  // Actions
  const fetchMenus = async (params?: {
    page?: number;
    pageSize?: number;
    name?: string;
    path?: string;
    acl?: string;
  }) => {
    loading.value = true;
    error.value = null;
    try {
      const response = await menuApi.getMenus(params);
      console.log("response ————》", response);
      menus.value = response.data.list;
      return {
        data: response.data.list,
        total: response.data.total,
        page: response.data.page,
        pageSize: response.data.pageSize,
        totalPages: response.data.totalPages,
      };
    } catch (err) {
      error.value = err instanceof Error ? err.message : "获取菜单列表失败";
      console.error("Failed to fetch menus:", err);
      throw err;
    } finally {
      setTimeout(() => (loading.value = false), 300);
    }
  };

  const createMenu = async (data: CreateMenuRequest) => {
    loading.value = true;
    error.value = null;
    try {
      const response = await menuApi.createMenu(data);
      menus.value.push(response.data);
      return response.data;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "创建菜单失败";
      console.error("Failed to create menu:", err);
      throw err;
    } finally {
      setTimeout(() => (loading.value = false), 300);
    }
  };

  const updateMenu = async (id: number, data: UpdateMenuRequest) => {
    loading.value = true;
    error.value = null;
    try {
      const response = await menuApi.updateMenu(id.toString(), data);
      const index = menus.value.findIndex((m) => m.id === id);
      if (index !== -1) {
        menus.value[index] = response.data;
      }
      return response.data;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "更新菜单失败";
      console.error("Failed to update menu:", err);
      throw err;
    } finally {
      setTimeout(() => (loading.value = false), 300);
    }
  };

  const deleteMenu = async (id: number) => {
    loading.value = true;
    error.value = null;
    try {
      await menuApi.deleteMenu(id.toString());
      menus.value = menus.value.filter((m) => m.id !== id);
    } catch (err) {
      error.value = err instanceof Error ? err.message : "删除菜单失败";
      console.error("Failed to delete menu:", err);
      throw err;
    } finally {
      setTimeout(() => (loading.value = false), 300);
    }
  };

  const getMenuDetail = async (id: number) => {
    loading.value = true;
    error.value = null;
    try {
      const response = await menuApi.getMenu(id.toString());
      menuDetail.value = response.data;
      return response.data;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "获取菜单详情失败";
      console.error("Failed to get menu detail:", err);
      throw err;
    } finally {
      setTimeout(() => (loading.value = false), 300);
    }
  };

  const clearError = () => (error.value = null);
  const setCurrentMenu = (menu: Menu | null) => (currentMenu.value = menu);
  const clearMenuDetail = () => (menuDetail.value = null);

  return {
    menus,
    loading,
    error,
    currentMenu,
    menuDetail,
    menuCount,
    fetchMenus,
    createMenu,
    updateMenu,
    deleteMenu,
    getMenuDetail,
    clearError,
    setCurrentMenu,
    clearMenuDetail,
  };
});
